package primegen

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import primegen.candidate.generation.GenFactory;
import primegen.candidate.generation.impl.GenCoordinator;

class PrimeGen {

	// we'll use the primes <200 with the algorithm ignoring even candidates, so 2 is omitted.
	ArrayList<Long> seedPrimes;
	GenCoordinator producers;

	PrimeGen() {
		seedPrimes = loadSeedPrimes()
		producers = GenFactory.createGenCoordinator(seedPrimes)
	}

	def start() {
		producers.start()
	}
	
	def stop() { 
		producers.stop()
	}


	ArrayList<Long> loadSeedPrimes() {
		def seedPrimes
		def input = this.getClass().getClassLoader().getResourceAsStream('primegen/seedPrimes10000.txt')
		if (input == null) {
			seedPrimes = [2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L ]
		}
		else {
			def reader = new InputStreamReader(input)
			def lines = reader.readLines()
			seedPrimes = new ArrayList<Long>()
			lines.each {
				it.eachMatch(/\d+/) {
					seedPrimes.add(Long.parseLong(it))
				}
			}
		}
		return seedPrimes
	}

	static main(args) {
		println 'Starting...'
		PrimeGen pg = new PrimeGen()
		pg.start()
		
		synchronized(pg) {
			pg.wait() 
		}
	}

}
